﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ZLCMS.Data.Model;
using NHibernate.Criterion;
using NHibernate;
using DotNet.Common.NHibernateUtil;
using Newtonsoft.Json;

namespace ZLCMS.Web.Interface.weixin.Html
{
    public partial class WxVote : System.Web.UI.Page
    {
        public WxVoteConfig _voteDb;
        public int _totalCount;
        public int _count;
        //   public WxVoteLog _voteLog { get; set; }

        public string ErrorMsg { get; set; }
        //public string VotedItem
        //{
        //    get
        //    {
        //        return _voteLog != null ? _voteLog.VoteItem : "";
        //    }
        //}
        protected void Page_Load(object sender, EventArgs e)
        {
            using (ISession session = SessionFactoryHelper.CreateSession())
            {
                _voteDb = session.CreateCriteria<WxVoteConfig>().Add(Expression.Eq("Id", Convert.ToInt32(Request["uid"]))).Add(Expression.Eq("OwnerAccount", Request["account"])).UniqueResult<WxVoteConfig>();

                _count = Convert.ToInt32(session.CreateCriteria<WxVoteLog>().Add(Expression.Eq("OwnerAccount", Request["account"])).Add(Expression.Eq("VoteId", _voteDb.Id)).SetProjection(Projections.RowCount()).UniqueResult());

                if (_voteDb.RuleDay == 0)
                {
                    _totalCount = Convert.ToInt32(session.CreateQuery("select count(*) from WxVoteLog where VoteId=:vid and WxId=:wxid ").SetParameter("vid", _voteDb.Id).SetParameter("wxid", Request["openid"]).UniqueResult());
                }
                else
                {
                    _totalCount = Convert.ToInt32(session.CreateQuery("select count(*) from WxVoteLog where VoteId=:vid and WxId=:wxid and OptDate> :time").SetParameter("vid", _voteDb.Id).SetParameter("wxid", Request["openid"]).SetParameter("time", DateTime.Now.AddDays(-1 * _voteDb.RuleDay)).UniqueResult());
                }

                //游戏是否开启
                if (_voteDb.Status == "SETTING" || _voteDb.BeginDate > DateTime.Now)
                {
                    ErrorMsg = "对不起,活动还未开启";
                }

                //游戏是否停止
                else if (_voteDb.Status == "PAUSE" || _voteDb.Status == "END" || _voteDb.EndDate.AddDays(1).Date < DateTime.Now)
                {
                    ErrorMsg = "对不起,活动已结束";
                }
                Repeater1.DataSource = session.CreateQuery("from WxVoteItem where ConfigId=:cid")
                    .SetParameter("cid", _voteDb.Id)
                    .List();
            }
            Repeater1.DataBind();

            if (_voteDb == null)
            {
                return;
            }
            else
            {
                //itemList=JsonConvert.DeserializeObject<WeixinVoteItemData[]>(_voteDb.VoteData);
            }

            if (_voteDb == null)
            {
                throw new Exception("找不到对应信息.");
            }
        }

        protected int GetVoteCounter(object item)
        {
            int result = 0;
            using (ISession session = SessionFactoryHelper.CreateSession())
            {
                result = Convert.ToInt32(session.CreateSQLQuery("select count(*) from wx_vote_log where vote_id=:vid and vote_item=:item and owner_account=:acc")
                    .SetParameter("vid", _voteDb.Id)
                    .SetParameter("item", item.ToString())
                    .SetParameter("acc", Request["account"])
                    .UniqueResult());
            }
            return result;
        }

    }
}